package com.whit.nuoge.servlet;


import com.whit.nuoge.pojo.User;
import com.whit.nuoge.service.UserService;
import com.whit.nuoge.service.impl.UserServiceImpl;
import com.whit.nuoge.utils.WebUtils;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

@WebServlet(value = "/manager/fileUpload")
public class FileUploadServlet extends HttpServlet {

    private UserService userService = new UserServiceImpl();

    private String localPath = "c:/upload/nuoge";

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        boolean isMultipart = ServletFileUpload.isMultipartContent(req);
        HashMap<String, String> formItemsMap = new HashMap<String, String>();
        if (isMultipart) {
            try {
                FileItemFactory fileItemFactory = new DiskFileItemFactory();
                ServletFileUpload fileUpload = new ServletFileUpload(fileItemFactory);
                List<FileItem> fileItems = fileUpload.parseRequest(req);
                System.out.println("表单项个数：" + fileItems.size());
                for (FileItem fileItem : fileItems) {
                    if (fileItem.isFormField()) {

                        formItemsMap.put(fileItem.getFieldName(), fileItem.getString("UTF-8"));
                    } else {
                        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
                        File f = new File(localPath + "/" + uuid + "_"
                                + fileItem.getName());
                        System.out.println("file 路径：" + f);
                        if (!"".equals(fileItem.getName())) {
                            fileItem.write(f);
                        }
                        formItemsMap.put(fileItem.getFieldName(), uuid + "_" + fileItem.getName());
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            System.out.println("不是合法的文件上传！！");
        }
        System.out.println("formItemsMap=");
        System.out.println(formItemsMap);
        //修改，没有选择图片,picurl字段
        if (formItemsMap.get("picurl").endsWith("_")) {
            formItemsMap.put("picurl", formItemsMap.get("prepicurl"));
        }
        //修改，没有选择图片，weixinpicurl字段
        if (formItemsMap.get("weixinpicurl").endsWith("_")) {
            formItemsMap.put("weixinpicurl", formItemsMap.get("preweixinpicurl"));
        }
        System.out.println("formItemsMap=");
        System.out.println(formItemsMap);
        if (formItemsMap != null && "user".equals(formItemsMap.get("actionType"))) {
            //User 对象
            User user = WebUtils.copyParamToBean(new User(), formItemsMap);

            String page = formItemsMap.get("pageNo");
            if ("update".equals(formItemsMap.get("action"))) {
                userService.update(user);
            }
            if ("add".equals(formItemsMap.get("action"))) {
                userService.save(user);
                page = Integer.MAX_VALUE +"";
            }

            String queryString = formItemsMap.get("queryString");
            if (queryString != null && queryString != "") {
                queryString = URLEncoder.encode(queryString, "UTF-8");
            }

            resp.sendRedirect(req.getContextPath() + "/manager/userServlet?action=queryForPageItemsByQueryString&pageNo="
                    + page + "&queryString=" + queryString);

            //end User对象
        }


    }


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
